﻿Public Class FormOutRecord
    Dim objpw_ck As New clsPWck
    Dim objGjbMst As New clsGjbMst
    Dim objFlbMst As New clsFlbMst
    Dim txt As TextBox
    Private Sub FormOutRecord_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.AddDays(-1).ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearce_Click(sender As Object, e As EventArgs) Handles btnSearce.Click
        setDgvData()
    End Sub
    Private Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dtPw As DataTable = objpw_ck.getPwCkOutList(txtStartDate.Text.Trim() + " 00:00:00.000",
                                                              txtEndDate.Text.Trim() + " 23:59:59.999",
                                                              txtCode.Text.Trim())
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtPw
        Else
            MsgBox("请输入开始时间和结束时间", vbYes, "提示")
        End If
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        Dim dtDetails As New DataTable
        If dgv.SelectedRows.Count > 0 Then
            dtDetails = objpw_ck.getPwCkOutDetailsList(dgv.SelectedRows(0).Cells("ck_out_code").Value.ToString())
            dtDetails.Columns.Add("oper_type_name")
            dtDetails.Columns.Add("validity_name")
            dtDetails.Columns.Add("size")
            If dtDetails.Rows.Count > 0 Then
                For Each row As DataRow In dtDetails.Rows
                    If row("oper_type") = "0" Then
                        row("oper_type_name") = "骨架板"
                        If DBNull.Value.Equals(row("material_code")) = False Then
                            Dim dtgjbmst As DataTable = objGjbMst.selectAll(row("material_code"))
                            row("size") = dtgjbmst.Rows(0)("size")
                        End If
                    Else
                        row("oper_type_name") = "法兰板"
                        If DBNull.Value.Equals(row("material_code")) = False Then
                            Dim dtflbmst As DataTable = objFlbMst.selectAll(row("material_code"))
                            row("size") = dtflbmst.Rows(0)("size")
                        End If
                    End If
                    If row("validity") = "0" Then
                        row("validity_name") = "无效"
                    Else
                        row("validity_name") = "有效"
                    End If
                Next
            End If
        End If
        dgv1.AutoGenerateColumns = False
        dgv1.DataSource = dtDetails
    End Sub

    Private Sub btnValidity_Click(sender As Object, e As EventArgs) Handles btnValidity.Click
        If dgv1.SelectedRows.Count > 0 Then
            If dgv1.SelectedRows(0).Cells("Col_validity").Value.ToString().Equals("1") Then
                Dim rtn As Integer = objpw_ck.updateOutDetailsVlidity(dgv1.SelectedRows(0).Cells("Col_oper_type").Value.ToString(), "0",
                                                                     dgv1.SelectedRows(0).Cells("Col_sys_id").Value.ToString(),
                                                                     dgv1.SelectedRows(0).Cells("ColBatchNo").Value.ToString(),
                                                                     dgv1.SelectedRows(0).Cells("Col_type_value").Value.ToString(),
                                                                     dgv1.SelectedRows(0).Cells("Col_out_num").Value.ToString())
                If rtn <> -2 Then
                    MsgBox("作废成功")
                    setDgvData()
                    CType(Owner, FormMain).setDgvData()
                Else
                    MsgBox("作废失败")
                End If
            Else
                MsgBox("该出库记录已作废")
            End If
        End If
    End Sub

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

End Class